package com.htudata.home.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import com.htudata.common.enums.RequestSeqEnum;
import com.htudata.convention.domain.ResponseResult;
import com.htudata.core.util.ResultGenerator;
import com.htudata.home.mapper.FctMapper;
import com.htudata.home.mapper.MntEqpMapper;
import com.htudata.home.service.IFctService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FctService implements IFctService {

  DateTimeFormatter dtf08 = DateTimeFormatter.ofPattern("yyyy-MM-dd 08:00");
  
  @Autowired
  private FctMapper fctMapper;
  
  @Autowired
  private MntEqpMapper mntEqpMapper;

  @Override
  public JSONObject selectMeteoPublicSatisfct() throws Exception {
    JSONObject jsonObject = new JSONObject();
    JSONArray mPSatisfct = this.fctMapper.selectMeteoPublicSatisfct();
    JSONArray mPSatisfctDetail = selectMeteoPublicSatisfctDetail();
    List<JSONObject> mPSatisfctDetailList = null;
    Map<String, List<JSONObject>> mPSatisfctDetailMap = null;
    if (mPSatisfctDetail != null) {
      mPSatisfctDetailList = (List<JSONObject>)JSON.parse(mPSatisfctDetail.toJSONString());
      mPSatisfctDetailMap = (Map<String, List<JSONObject>>)mPSatisfctDetailList.stream().collect(Collectors.groupingBy(x -> x.getString("dataTime")));
    } 
    jsonObject.put("mPSatisfct", mPSatisfct);
    jsonObject.put("mPSatisfctDetail", mPSatisfctDetailMap);
    return jsonObject;
  }

  @Override
  public JSONObject selectFctData(String time)  throws Exception{
    JSONObject jsonObject = new JSONObject();
    JSONArray fctDataStatis = selectFctDataStatis();
    List<JSONObject> fctDataStatisList = null;
    Map<String, List<JSONObject>> fctDataStatisMap = null;
    if (fctDataStatis != null) {
      fctDataStatisList = (List<JSONObject>)JSON.parse(fctDataStatis.toJSONString());
      fctDataStatisMap = (Map<String, List<JSONObject>>)fctDataStatisList.stream().collect(Collectors.groupingBy(x -> x.getString("fct")));
    } 
    jsonObject.put("fctOverview", selectFctDataOverview());
    jsonObject.put("fctDataStatis", fctDataStatisMap);
    jsonObject.put("fctVerifyStatis", selectFctVerifyStatis(time));
    return jsonObject;
  }

  @Override
  public String selectFctDataJSON(String time)  throws Exception{
    ResponseResult result = null;
    try {
      result = ResultGenerator.genResult(RequestSeqEnum.SEQ_011.getCode(), selectFctData(time));
    } catch (Exception e) {
       throw e;
    } 
    return JSON.toJSONString(result);
  }

  @Override
  public String selectMeteoPublicSatisfctJSON()  throws Exception{
    ResponseResult result = null;
    try {
      result = ResultGenerator.genResult(RequestSeqEnum.SEQ_012.getCode(), selectMeteoPublicSatisfct());
    } catch (Exception e) {
      throw e;
    } 
    return JSON.toJSONString(result);
  }

  @Override
  public JSONArray selectFctDataOverview()  throws Exception{
    try {
      return this.fctMapper.selectFctDataOverview();
    } catch (Exception e) {
      throw e;
    } 
  }

  @Override
  public JSONArray selectFctDataStatis()  throws Exception{
    try {
      return this.fctMapper.selectFctDataStatis();
    } catch (Exception e) {
      throw e;
    } 
  }

  @Override
  public Map<String, List<JSONObject>> selectFctVerifyStatis(String time)  throws Exception{
    try {
      int plus = -1;
      Map<String, List<JSONObject>> rootMap = new HashMap<>();
      List<JSONObject> array = null;
      String[] root = { "heavy_rain", "sunny_rain", "temp_max", "temp_min" };
      String[] fct = { "GRAPES", "SUBJ_FCT", "EC" };
      for (int i = 0; i < 4; i++) {
        array = new ArrayList<>();
        JSONObject mObject = null;
        for (int j = 0; j < 3; j++) {
          for (int q = 0; q < 7; q++) {
            mObject = new JSONObject();
            mObject.put("fctEle", root[i]);
            int f = 7 - q - 1;
            if ("week".equals(time)) {
              mObject.put("dataTime", this.dtf08.format(LocalDateTime.now().plusWeeks((plus * f))));
            } else if ("year".equals(time)) {
              mObject.put("dataTime", this.dtf08.format(LocalDateTime.now().plusYears((plus * f))));
            } else if ("month".equals(time)) {
              mObject.put("dataTime", this.dtf08.format(LocalDateTime.now().plusMonths((plus * f))));
            } else {
              mObject.put("dataTime", this.dtf08.format(LocalDateTime.now().plusDays((plus * f))));
            } 
            mObject.put("fct", fct[j]);
            mObject.put("units", "%");
            mObject.put("value", Integer.valueOf(75 + (new Random()).nextInt(10)));
            array.add(mObject);
          } 
        } 
        rootMap.put(root[i], array);
      } 
      return rootMap;
    } catch (Exception e) {
      throw e;
    } 
  }

  @Override
  public JSONArray selectMeteoPublicSatisfctDetail()   throws Exception{
    try {
      return this.fctMapper.selectMeteoPublicSatisfctDetail();
    } catch (Exception e) {
      throw e;
    } 
  }
}
